package com.itheima.health.service.impl;


import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderParam;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author guoshijie
 * @Date 2022/6/8 16:39
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public OrderParam findById(Integer id) {
        log.info("[根据id查询预约信息]data：{}", id);
       String memberName = orderDao.findMemberName(id);
        String setMealName = orderDao.findSetMealName(id);
        Order order =  orderDao.findOrder(id);
        OrderParam orderParam = new OrderParam();
        orderParam.setMember(memberName);
        orderParam.setSetmeal(setMealName);
        orderParam.setOrderDate(order.getOrderDate());
        orderParam.setOrderType(order.getOrderType());

        return orderParam;
    }

    @Override
    public Result order(OrderParam param) {
        log.info("[添加预约]");
        // 检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
          Date orderDate = param.getOrderDate();
        OrderSetting orderSetting = orderDao.selectByOrderDate(orderDate);
        //检查是否设置了预约
        if(orderSetting==null){
            log.info("[当天没有设置预约]");
             return new Result(false,MessageConst.ORDER_FULL);
         }
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        //预约人满了
        if (reservations>number){
            log.info("[预约人满了]");
            return new Result(false,MessageConst.ORDER_FULL);
        }
        //获取手机号
        String telephone = param.getTelephone();
        //根据手机号获取预约人信息
        Member member = memberDao.getone(telephone);


        //获取套餐id
        Integer setMealId = param.getSetMealId();
        //检查用户是否重复预约
        if (member != null) {
            //获取预约人id
            Integer memberId = member.getId();
            Integer find = orderDao.findByCondition(memberId, orderDate, setMealId);
            if (find > 0) {
                log.info("[用户重复预约]");
                return new Result(false, MessageConst.HAS_ORDERED);
            }
        } else {
            //不是会员，自动完成注册
            member = new Member();
            member.setName(param.getName());
            member.setSex(param.getSex());
            member.setIdCard(param.getIdCard());
            member.setPhoneNumber(param.getTelephone());
            member.setRegTime(new Date());
            memberDao.add(member);//^
        }
        // 预约成功，提交预约体检信息
        Order order = new Order();
        order.setMemberId(member.getId());//会员id
        order.setOrderDate(param.getOrderDate());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);//方法 微信预约
        order.setOrderStatus(Order.ORDERSTATUS_NO);// 预约 默认味道真
        order.setSetmealId(param.getSetMealId());//选择的套餐id
        orderDao.add(order);//^
        // 更新当日的已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderDao.editReservations(orderSetting);
        return new Result(true,MessageConst.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Order findOrderId(OrderParam param) {
        String telephone = param.getTelephone();
        Member member = memberDao.getone(telephone);
        Integer memberId = member.getId();

        Order id = orderDao.findByOrder(memberId, param.getOrderDate(), param.getSetMealId());

        return id;
    }

    @Override
    public List<Map<String, Object>> countSetmeal() {
        return orderDao.countSetMeal();
    }

}
